{% extends "base.html" %}

{% block title %}CNInfo - 问董秘{% endblock %}

{% block page_title %}问董秘{% endblock %}
{% block page_subtitle %}上市公司投资者互动问答（沪市/深市）{% endblock %}

{% block extra_css %}
<style>
    .tabs { padding: 1rem 1.5rem 0; }
    .qa-list { padding: 1rem 1.5rem; display: grid; grid-template-columns: 1fr; gap: 12px; }
    .qa-card { background: #fff; border: 1px solid #f0ead6; border-radius: 12px; padding: 14px; box-shadow: 0 2px 8px rgba(0,0,0,0.04); }
    .qa-header { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
    .qa-avatar { width: 36px; height: 36px; border-radius: 50%; object-fit: cover; background: #f5f5f5; }
    .qa-stock { font-weight: 600; color: #5d4e37; text-decoration: none; }
    .qa-time { color: #888; font-size: 12px; }
    .qa-q, .qa-a { margin-top: 8px; white-space: pre-wrap; line-height: 1.65; }
    .qa-q .tag, .qa-a .tag { display: inline-block; font-size: 12px; padding: 2px 8px; border-radius: 12px; margin-right: 8px; }
    .qa-q .tag { background: #ffe6e6; color: #b33; }
    .qa-a .tag { background: #e6f2ff; color: #247; }
    .qa-meta { margin-top: 6px; color: #666; font-size: 12px; }
    @media (max-width: 768px) { .qa-list { padding: 0.8rem; } }
    
    .tab-link { margin-right:12px; padding:6px 12px; border-radius:16px; text-decoration:none; }
    .tab-active { background:#8b7355; color:#fff; }
    .tab-inactive { background:#eee; color:#333; }

    .page-jump { margin-top:10px; text-align:center; }
    .jump-input { width:60px; padding:4px; border-radius:4px; border:1px solid #ccc; text-align:center; }
    .jump-button { padding:4px 8px; background:#8b7355; color:#fff; border:none; border-radius:4px; cursor:pointer; margin-left:5px; }
</style>
{% endblock %}

{% block content %}
<div class="tabs">
    <a href="{{ request.url.replace_query_params(market='sz', page=1) }}" class="tab-link {% if market!='sh' %}tab-active{% else %}tab-inactive{% endif %}">深市</a>
    <a href="{{ request.url.replace_query_params(market='sh', page=1) }}" class="tab-link {% if market=='sh' %}tab-active{% else %}tab-inactive{% endif %}">沪市</a>
</div>

<div class="qa-list">
    {% if data|length == 0 and market != 'sh' %}
        <div class="qa-card">当前仅支持沪市问答数据，请切换至“沪市”。</div>
    {% endif %}

    {% for item in data %}
    <div class="qa-card">
        <div class="qa-header">
            {% if item.stock_image %}
                <img class="qa-avatar" src="{{ item.stock_image }}" alt="{{ item.stock_name }}">
            {% else %}
                <div class="qa-avatar"></div>
            {% endif %}
            <div>
                <a href="/c/{{ item.stock_code }}" class="qa-stock">{{ item.stock_name }}（{{ item.stock_code }}）</a>
                <div class="qa-time">提问时间：<span class="q-time" data-ts="{{ item.question_timestamp }}">{{ item.question_time }}</span></div>
            </div>
        </div>
        <div class="qa-q"><span class="tag">提问</span>{{ item.question_text }}</div>
        {% if item.answer_text %}
        <div class="qa-a"><span class="tag">回答</span>{{ item.answer_text }}</div>
        <div class="qa-meta">回答时间：<span class="a-time" data-ts="{{ item.answer_timestamp }}">{{ item.answer_time }}</span></div>
        {% else %}
        <div class="qa-meta">（暂未回答）</div>
        {% endif %}
    </div>
    {% endfor %}
</div>
{% endblock %}

{% block pagination %}
{% if pages and page %}
<div class="pagination">
    <h3>共 {{pages}} 页</h3>
    <div class="pagination-links">
        {% if page > 1 %}
        <a href="{{ request.url.replace_query_params(page=page-1, market=market or 'sh') }}">← 上一页</a>
        {% endif %}
        <span class="current-page">当前为第 {{page}} 页</span>
        {% if page < pages %}
        <a href="{{ request.url.replace_query_params(page=page+1, market=market or 'sh') }}">下一页 →</a>
        {% endif %}
    </div>
    <div class="page-jump">
        <form method="get" action="{{ request.path_url }}">
            {% for key, value in request.query_params.items() if key != 'page' %}
            <input type="hidden" name="{{key}}" value="{{value}}">
            {% endfor %}
            <input type="number" name="page" min="1" max="{{pages}}" value="{{page}}" class="jump-input">
            <button type="submit" class="jump-button">跳转</button>
        </form>
    </div>
</div>
{% endif %}
{% endblock %}

{% block extra_js %}
<script>
function formatDate(ts){
    if(!ts){return ''}
    const v = parseInt(ts,10)
    if(isNaN(v)){return ''}
    const ms = v>1e12? v: v*1000
    const d = new Date(ms)
    const y=d.getFullYear(), m=String(d.getMonth()+1).padStart(2,'0'), da=String(d.getDate()).padStart(2,'0')
    const hh=String(d.getHours()).padStart(2,'0'), mm=String(d.getMinutes()).padStart(2,'0'), ss=String(d.getSeconds()).padStart(2,'0')
    return `${y}-${m}-${da} ${hh}:${mm}:${ss}`
}
window.addEventListener('DOMContentLoaded', ()=>{
    document.querySelectorAll('.q-time').forEach(el=>{
        const ts = el.getAttribute('data-ts')
        const f = formatDate(ts)
        if(f){ el.textContent = f }
    })
    document.querySelectorAll('.a-time').forEach(el=>{
        const ts = el.getAttribute('data-ts')
        const f = formatDate(ts)
        if(f){ el.textContent = f }
    })
})
</script>
{% endblock %}


